#include "serwermanager.h"

SerwerManager::SerwerManager(QString ip, int port, int localport, int id,QWidget *parrent):
        QWidget(parrent),
        menu(new QWidget),
        menulay(new QVBoxLayout(menu)),
        main(new QVBoxLayout(this)),
        tab_widget(new QTabWidget()),
        basic_info_1(new QWidget()),
        basic_players_2(new QWidget()),
        dodaj_do_bazy(new QPushButton("Dodaj Serwer")),
        serwer_is_set(false)
{

    //ip="89.248.165.13";
    //port=3455;
    main->addWidget(tab_widget);
    info=new SerwerInfo(ip,port,localport,id);
    serwer_is_set=true;
    init_basic_info_tab();
    init_basic_players_tab();
    info->start();
    setFixedSize(800,600);

}

void SerwerManager::send_rcon_password()
{
    QString pass(rcon_password->text());
    info->rcon_password=pass;
    info->send_datagram(RCON_CHALLENGE_RCON);
    browser->setText("32543");



}

void SerwerManager::init_basic_info_tab()
{
    QGridLayout *main1=new QGridLayout(basic_info_1);
    QVBoxLayout *layv11=new QVBoxLayout();
    QHBoxLayout *layh12=new QHBoxLayout();
    QVBoxLayout *layv13=new QVBoxLayout();
    QVBoxLayout *layv14=new QVBoxLayout();


    QGroupBox *box1a=new QGroupBox();
    box1a->setTitle("Basic Info");
    QGroupBox *box2a=new QGroupBox();
    box2a->setTitle("RCON Login");
    QGroupBox *box3a=new QGroupBox();
    box3a->setTitle("Serwer Rules");

    //info o serwerze z Labeli
    LabelInfo=info->LabelInfo;
    //tabela graczy "basic"
    players_table=info->players_table_basic;
    //debug
    rules=new QTableWidget;
    browser=info->bro;
    //label przy polu do hasla
    QLabel *lab1=new QLabel("RCON PASSWORD");
    //pole na haslo
    rcon_password=new QLineEdit();
    //przycisk do logowania
    login=new QPushButton();
    login->setText("Zaloguj rcon");
    status_rcon=new QLabel();

    layv11->addWidget(LabelInfo);
    box1a->setLayout(layv11);
    main1->addWidget(box1a,0,0);

    main1->addWidget(players_table,0,1);

    layh12->addWidget(lab1,Qt::AlignTop);
    layh12->addWidget(rcon_password,Qt::AlignTop);
    //layh12->addWidget(status_rcon,Qt::AlignTop);

    layv13->addLayout(layh12,Qt::AlignTop);
    layv13->addWidget(login,Qt::AlignTop);
    layv13->addWidget(status_rcon,0,Qt::AlignCenter);
    box2a->setLayout(layv13);
    //main1->addWidget(box2a,1,0);

    wyjscie=new QPushButton("Wyjscie");
    wyjscie->setFixedWidth(100);
    QVBoxLayout *wyjLay2=new QVBoxLayout();

    wyjLay2->addWidget(wyjscie,0,Qt::AlignCenter);

    box3a->setTitle("Serwer Rules");
    QVBoxLayout *tempp=new QVBoxLayout();
    //tempp->addWidget(box1a);
    tempp->addWidget(box2a);
    tempp->addLayout(wyjLay2);

    main1->addLayout(tempp,1,0);



    //layv14->addWidget(browser);
    layv14->addWidget(browser);
    box3a->setLayout(layv14);
    main1->addWidget(box3a,1,1);
    //main1->addWidget(players_table,0,1);

    connect(login,SIGNAL(clicked()),this,SLOT(send_rcon_password()));
    connect(info,SIGNAL(rcon_valid(int,bool)),this,SLOT(rcon_status(int,bool)));
    connect(wyjscie,SIGNAL(clicked()),info,SLOT(serwer_try_exit()));
    tab_widget->addTab(basic_info_1,QIcon("icons/blueicon.png"),"Serwer Info");


}

void SerwerManager::init_basic_players_tab()
{
    QVBoxLayout *main=new QVBoxLayout(basic_players_2);
    basic_player_graph=info->basic_player_graph;

    save_image_players=new QPushButton();
    save_image_players->setText("Zapisz");
    connect(save_image_players,SIGNAL(clicked()),this->basic_player_graph,SLOT(choose_file()));

    QGroupBox *box=new QGroupBox();
    box->setTitle("Wykres Graczy");
    QVBoxLayout *lay=new QVBoxLayout();
    lay->addWidget(basic_player_graph,0,Qt::AlignCenter);
    box->setLayout(lay);

    //players_table=new QTableWidget();
    //main->addWidget(players_table,0,Qt::AlignCenter);
    //main->addWidget(basic_player_graph,0,Qt::AlignCenter);
    main->addWidget(save_image_players);
    main->addWidget(box);
    tab_widget->addTab(basic_players_2,QIcon("icons/blueicon.png"),"players");
}

void SerwerManager::init_rcon_console_tab()
{
    rcon_console_3=new QWidget();
    QVBoxLayout *main=new QVBoxLayout(rcon_console_3);

    QGroupBox *cons_out=new QGroupBox();
    cons_out->setTitle("Rcon Output");
    QGroupBox *cons_in=new QGroupBox();
    cons_in->setTitle("Rcon Input");

    QHBoxLayout *in=new QHBoxLayout();
    QHBoxLayout *out=new QHBoxLayout();


    console_output=info->console_output;
    console_input=info->console_input;
    send=info->send;
    clear=info->clear;

    in->addWidget(console_input);
    in->addWidget(send);
    in->addWidget(clear);

    out->addWidget(console_output);

    cons_in->setLayout(in);
    cons_out->setLayout(out);

    main->addWidget(cons_out);
    main->addWidget(cons_in);

    tab_widget->addTab(rcon_console_3,QIcon("icons/redicon.png"),"RCON");

}

void SerwerManager::rcon_status(int id, bool st)
{
    if(st==true)
    {
        status_rcon->setText("Zalogowany !");
        QString pass(rcon_password->text());
        info->rcon_password=pass;
        info->init_strona3();
        init_rcon_console_tab();
    }
    else
    {
        status_rcon->setText("Zle haslo !");
    }
}


